//user_register.html
$(function() {

	// 校验输入的手机号
	var phoneReg = /^1[3|4|5|7|8][0-9]{9}$/;// 手机号正则表达式
	var user_phoneFlag = $("#user_phoneFlag");
	function verifyPhone() {
		var phone = $("#u_telephone").val();// 获取用户输入的手机号
		if (phone == null || phone == "") {
			$("#phoneRegTip").css("display", "block");
			user_phoneFlag.attr("value", "false");// 账户输入flag,false
			return false;
		} else if (phone.replace(/(^\s*)|(\s*$)/g, "") == "") {
			$("#phoneRegTip").css("display", "block");
			user_phoneFlag.attr("value", "false");
			return false;
		} else if (!phoneReg.test(phone)) {
			$("#phoneRegTip").css("display", "block");
			user_phoneFlag.attr("value", "false");
			return false;
		}
		return true;
	}
	// 动态校验手机格式
	$("#u_telephone").bind("input propertychange change", function(event) {
		$("#phoneUsedTip").css("display", "none");
		$("#phoneMsgDiv").css("display", "none");
		var phone = $("#u_telephone").val();// 获取用户输入的手机号
		if (!phoneReg.test(phone)) {
			$("#phoneRegTip").css("display", "block");
			user_phoneFlag.attr("value", "false");
		} else {
			$("#phoneRegTip").css("display", "none");
		}
	});
	// 校验手机号是否已被占用
	$("#u_telephone").blur(function() {
		var phone = $("#u_telephone").val();// 获取用户输入的手机号
		if (verifyPhone()) {
			$.ajax({
				type : 'post',
				url : '/tenement/verifyPhoneUsed.action',
				data : phone,
				contentType : "application/json;charset=utf-8",
				success : function(msg) {
					if (msg == "1") {
						$("#phoneUsedTip").css("display", "block");
						user_phoneFlag.attr("value", "false");
					} else {
						$("#phoneUsedTip").css("display", "none");
						$("#phoneMsgDiv").css("display", "block");
						user_phoneFlag.attr("value", "true");// 该手机号可以使用
					}
				}
			})
		}
	})

	// 发送手机验证码
	var sendPhoneCodeBtn = $("#sendPhoneCodeBtn");
	$("#sendPhoneCodeBtn").click(function() {
		settime();
		var phoneFlag = user_phoneFlag.attr("value");
		if (phoneFlag == "true") {
			var phoneNumber = $('#u_telephone').val();// 获取输入的新手机号
			// 如果手机号格式正确，未被绑定，则可以发生验证码
			$.ajax({
				type : 'post',
				url : '/tenement/sendPhoneCode.action',
				data : phoneNumber,
				contentType : "application/json;charset=utf-8",
				success : function(msg) {
					console.log("【手机验证码发送成功】" + msg);
				},
				error : function(msg) {
					console.log("【系统异常】");
				}
			})
		} else {
			return;
		}
	});
	// 获取验证码按钮可以点击
	function sendPhoneCodeBtnYes() {
		sendPhoneCodeBtn.removeClass("disabled");
		sendPhoneCodeBtn.attr("disabled", false);
	}
	// 获取验证码按钮不可点击
	function sendPhoneCodeBtnNo() {
		sendPhoneCodeBtn.attr("disabled", true);
		sendPhoneCodeBtn.addClass("disabled");
	}
	// 发送验证码倒计时60s
	var countdown = 10;
	function settime() {
		if (countdown == 0) {
			sendPhoneCodeBtn.html("获取验证码");
			if (user_phoneFlag.attr("value") == "true") {
				sendPhoneCodeBtnYes();
			} else {
				sendPhoneCodeBtnNo();
			}
			countdown = 10;
			return;
		} else {
			sendPhoneCodeBtn.html("重新发送(" + countdown + ")");
			sendPhoneCodeBtnNo();
			countdown--;
		}
		setTimeout(settime, 1000);
	}

	function verifyPhoneCode() {
		var code = $('#sms').val();
		var data = {
			"code" : code,
			"phoneNumber" : $("#u_telephone").val()
		};
		if (code != null && code != "") {
			$.ajax({
				type : 'post',
				url : '/tenement/verifyPhoneCode.action',
				data : JSON.stringify(data),
				contentType : "application/json;charset=utf-8",
				success : function(msg) {
					console.log("msg:" + msg);
					if (msg == "1") {
						$("#phoneCodeTip").css("display", "none");
						$("#smsFlag").attr("value", "true");
					} else {
						$("#phoneCodeTip").css("display", "block");
						$("#smsFlag").attr("value", "false");
					}
				}
			})
		}
	}
	$('#sms').blur(function() {
		verifyPhoneCode();
	})

	// 校验密码
	// 校验输入的第一次密码
	var pwdReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,21}$/; // 6-21字母和数字组成，不能是纯数字或纯英文；
	$("#pwd1").bind("input propertychange change", function(event) {
		verifyPwd1();
	});
	function verifyPwd1() {
		var pwd1_val = $("#pwd1").val();
		var pwd1Flag = $("#pwd1Flag");
		if (!pwdReg.test(pwd1_val)) {
			$("#pwd1Tip").css("display", "block");
			pwd1Flag.attr("value", "false");
		} else {
			$("#pwd1Tip").css("display", "none");
			pwd1Flag.attr("value", "true");
		}
	}
	// 判断两次输入的密码是否一致
	function verifyPwd2() {
		verifyPwd1();
		var pwd1_val = $("#pwd1").val();
		var pwd2_val = $("#pwd2").val();
		var pwd2Flag = $("#pwd2Flag");
		// 校验第一次输的密码是否为空
		if (pwd1_val == pwd2_val) {
			$("#pwd2Tip").css("display", "none");
			pwd2Flag.attr("value", "true");
		} else {
			$("#pwd2Tip").css("display", "block");
			pwd2Flag.attr("value", "false");
		}
	}
	$("#pwd2").bind("input propertychange change", function(event) {
		verifyPwd2();
	});

	// 注册提交数据
	$("#signupBtn").click(function() {

		var user_phoneFlag = $("#user_phoneFlag").attr("value");
		var imgCodeFlag = $("#imgCodeFlag").attr("value");
		var smsFlag = $("#smsFlag").attr("value");
		var pwd1Flag = $("#pwd1Flag").attr("value");
		var pwd2Flag = $("#pwd2Flag").attr("value");
		if (user_phoneFlag == "false") {
			verifyPhone();
			return;
		} else if (imgCodeFlag == "false") {
			$("#imgCodeTip").css("display", "block");
			return;
		} else if (smsFlag == "false") {
			$("#phoneCodeTip").css("display", "block");
			return;
		} else if (pwd1Flag == "false") {
			verifyPwd1();
			return;
		} else if (pwd2Flag == "false") {
			verifyPwd2();
			return;
		} else {
			// 传输数据
			var data = {
				"phone" : $("#u_telephone").val(),
				"pwd" : $("#pwd2").val()
			};
			$.ajax({
				type : 'post',
				url : '/tenement/userRegister.action',
				data : JSON.stringify(data),
				contentType : "application/json;charset=utf-8",
				success : function(msg) {
					if (msg == 1) {
						location.href="../../../user_login.action?pageType=register";
						刷新当前页面
					} else {
						alert("注册失败");
					}
				}
			})
		}

	})

});